package com.summarecon.qualitycontrol;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.summarecon.utility.MakeDir;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

public class InitActivity extends Activity{

	public static final String path="/Summarecon/database/temp/";
	public String DATABASE_NAME = "summarecon_db.db";
	public boolean resultCheck = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_init);
		MakeDir.createDirIfNotExists(path);
		checkDatabase(path);
	}

	public String getDataDir() throws Exception {
		return getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0).applicationInfo.dataDir;
	}

	public void checkDatabase(String path) {

		File database = getBaseContext().getDatabasePath(DATABASE_NAME);
		Log.i("pathInternal",database.toString());
		
		if (!database.exists()) {
			// check external file
			Toast.makeText(getBaseContext(), "Database not Exsist, now checking External storage", Toast.LENGTH_SHORT).show();

			File external_path = Environment.getExternalStoragePublicDirectory(path);
			File external_db = new File(external_path, DATABASE_NAME);

			if(!external_db.exists()){
				//return false;
				Toast.makeText(getBaseContext(), "Database not found, please Synchronize first", Toast.LENGTH_LONG).show();
			}else{
				Toast.makeText(getBaseContext(), "Copy Database from External storage", Toast.LENGTH_SHORT).show();
				copyFile(path);
				//CopyFile.copyDatabase(external_db);
				//load database
				//result = true;
			}

		} else {
			Toast.makeText(getBaseContext(), "Database Loaded", Toast.LENGTH_SHORT).show();
			nextAct();
			//load database
			//result = true;
		}
	}

	public void copyFile(String path){

		File path2 = Environment.getExternalStoragePublicDirectory(path);
		File file = new File(path2, DATABASE_NAME);
		Log.w("path",file.toString());

		String databaseDir = null;
		try {
			databaseDir = this.getDataDir();
			File InternalDir= new File(databaseDir, "/databases");
			Log.w("path",InternalDir.toString());
			if(!InternalDir.exists()){
				InternalDir.mkdir();
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		try {
			// Make sure the Pictures directory exists.
			//path2.mkdirs();

			// Very simple code to copy a picture from the application's
			// resource into the external file.  Note that this code does
			// no error checking, and assumes the picture is small (does not
			// try to copy it in chunks).  Note that if external storage is
			// not currently mounted this will silently fail.
			InputStream is = new FileInputStream(file);
			OutputStream os = new FileOutputStream(databaseDir + "/databases/" + DATABASE_NAME);

			byte[] data = new byte[is.available()];
			is.read(data);
			os.write(data);
			is.close();
			os.close();

			//file.delete();

			// Tell the media scanner about the new file so that it is
			// immediately available to the user.
			
			/*
			MediaScannerConnection.scanFile(this, new String[] { file.toString() }, null, new MediaScannerConnection.OnScanCompletedListener() {

				public void onScanCompleted(String path, Uri uri) {
					Log.i("DoneCopy","OK");
					Log.i("ExternalStorage", "Scanned " + path + ":");
					//Log.i("ExternalStorage", "-> uri=" + uri);
				}
			});
			*/
			nextAct();
		} catch (IOException e) {
			// Unable to create file, likely because external storage is
			// not currently mounted.
			Log.i("ExternalStorage", "Error writing " + file, e);

		}

	}
	public void nextAct(){	
		
		Intent myIntent = new Intent(getBaseContext(), LoginActivity.class);
		startActivity(myIntent);
		finish();
	}

}
